import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";

let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {

		},
		//确定收料 接口
		materialsReceiptStorage(that, params) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				// url:'/api/supplier/whWarehouse/materialsReceiptStorage',
				url: apiTest + '/supplier/whWarehouse/materialsReceiptStorage',
				data: params
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var result = data.result;
					var obj;
					if (result == 1) {
						that.tips(that, 'success', '操作成功');
						that.Model.tableData = [];
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//扫码入库接口
		codeEntryAjax(that, codeArr) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				// url:'/api/supplier/whWarehouse/materialsScanCode',
				url: apiTest + '/supplier/whWarehouse/materialsScanCode',
				data: {
					barCodes: codeArr,
				}
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result.length == 0) {
						that.tips(that, 'warning', '未找到相关条码');
					} else {
						for (let i = 0; i < result.length; i++) {
							obj = objDeepCopy(result[i]);
							that.Model.tableData.push(obj);
						}

					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},

		//回车或点击确定
		codeEntry() {
			var code = this.Model.searchCode;
			if (code.length == 0) {
				this.tips(this, 'warning', '请输入条码或者商品编码');
				return;
			}
			var tableData = this.Model.tableData;
			var codeIndexOf = this.valueInData(code, tableData);
			if (codeIndexOf == -1) {
				this.codeEntryAjax(this, [code]);
			} else {
				this.tips(this, 'warning', '条码已存在')
			}

		},
		//遍历 判断是否存在数组中
		valueInData(val, data) {
			var indexof = -1;
			data.forEach(function(value, index, array) {
				Object.keys(value).forEach(function(key) {
					// console.log(key,value[key]);
					if (key == 'barCode' && value[key] == val) {
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData() {
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableData = this.Model.tableData;
			if (TableSelectData.length == 0) {
				this.tips(this, 'warning', '请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value, index, array) {
					tableData.forEach(function(val, idx, arr) {
						if (value == val) {
							that.Model.tableData.splice(idx, 1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {

			});
		},
		//确定收料
		onConfirm() {
			var tableData = this.Model.tableData;
			if (tableData.length == 0) {
				this.tips(this, 'warning', '请选择需要确定收料的数据');
				return;
			} else {
				this.$confirm('此操作将确定收料, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					var obj = {};
					for (let i = 0; i < tableData.length; i++) {
						obj[tableData[i].barCode] = tableData[i].remark; 
					}
					this.materialsReceiptStorage(this, obj)
				}).catch(() => {

				});
			}

		},
		//


		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {

		},
		handleSizeChange(val) {

		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {
				if (newValue.query.id) {
					this.getDetailList(this, newValue.query.id);
				}
				//打开相应的tab和弹窗
				if (newValue.query.to == 'addPay') {
					this.toaddPay()
					this.Model.activeTab = '3';
				} else if (newValue.query.to == 'addGift') {
					this.toaddGift()
					this.Model.activeTab = '2';
				}
			},
			deep: true
		},

	},
	filters: {
		objFilters: function(value) {

		}
	}
}
export default Controller;
